from django.contrib import admin
from .models import Label, Essay, Picture, Comment, Series


class LabelAdmin(admin.ModelAdmin):
    fields = ['title']
    list_display = ['title']


class PictureInline(admin.StackedInline):
    model = Picture
    extra = 3
    fields = ['picture_name', 'picture_url', 'order', 'essay']


class EssayAdmin(admin.ModelAdmin):
    # fields = ['title', 'summary', 'series', 'content', 'rich_text', 'labels', 'creator']

    fieldsets = (
        ("笔记基本信息", {'fields': ['title', 'summary', 'series', 'labels', 'creator']}),
        ("笔记内容", {'fields': ['content', 'rich_text']})
    )

    inlines = [PictureInline]
    filter_horizontal = ('labels',)
    list_display = ['title', 'create_time', 'update_time', 'show_creator']

    def show_creator(self, obj):
        return obj.creator.username

    show_creator.short_description = '发布者'


class PictureAdmin(admin.ModelAdmin):
    fields = ['picture_name', 'picture_url', 'order', 'essay']
    list_display = ['picture_name', 'picture_url', 'show_essay', 'order', 'create_time', 'update_time']

    def show_essay(self, obj):
        return obj.essay.title

    show_essay.short_description = '所属笔记'


class CommentAdmin(admin.ModelAdmin):
    fields = ['nick_name', 'essay', 'parent_id', 'rich_text']
    list_display = ['nick_name', 'show_essay', 'create_time', 'update_time']

    def show_essay(self, obj):
        return obj.essay.title

    show_essay.short_description = '所属笔记'


class SeriesAdmin(admin.ModelAdmin):
    fields = ['series_name', 'series_description']
    list_display = ['series_name', 'create_time', 'update_time']


class SeriesInline(admin.StackedInline):
    model = Series
    extra = 1
    fields = ['series_name', 'series_description']


# 修改默认的header和title
admin.site.site_header = '笔记'
admin.site.site_title = '笔记'

admin.site.register(Label, LabelAdmin)
admin.site.register(Picture, PictureAdmin)
admin.site.register(Essay, EssayAdmin)
admin.site.register(Comment, CommentAdmin)
admin.site.register(Series, SeriesAdmin)
